using System;
using System.Windows.Forms;
using SVNClean.Modules;

namespace SVNClean.Windows {
    public partial class frm_Preferences : DevExpress.XtraEditors.XtraForm {
        /// <summary>
        /// Initializes a new instance of the <see cref="frm_Preferences"/> class.
        /// </summary>
        public frm_Preferences() {
            InitializeComponent();

            propertyGridControl1.SelectedObject = Modules.AppConfiguration.Current;
            this.LookAndFeel.SetSkinStyle(AppConfiguration.Current.DefaultSkin);
            AppConfiguration.Current.SkinChanged += new EventHandler(Current_SkinChanged);
        }

        #region "[rgn] Skin Actions "
        private Timer tmSkin;
        private void Current_SkinChanged(object sender, EventArgs e) {
            tmSkin = new Timer();
            tmSkin.Tick += new EventHandler(tmSkin_Tick);
            tmSkin.Start();
        }
        private void tmSkin_Tick(object sender, EventArgs e) {
            if (this.LookAndFeel.SkinName != AppConfiguration.Current.DefaultSkin) {
                DevExpress.LookAndFeel.UserLookAndFeel.Default.SetSkinStyle(AppConfiguration.Current.DefaultSkin);
                this.LookAndFeel.SetSkinStyle(AppConfiguration.Current.DefaultSkin);
            }
            tmSkin.Stop();
        }
        #endregion

        /// <summary>
        /// Handles the Load event of the frm_Preferences control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void frm_Preferences_Load(object sender, EventArgs e) {
            propertyGridControl1.FocusedRow = propertyGridControl1.Rows[0].ChildRows[0];
        }

        /// <summary>
        /// Handles the Click event of the btnSave control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnSave_Click(object sender, EventArgs e) {
            AppConfiguration.Current.SaveData();
            Close();
        }

        /// <summary>
        /// Handles the ItemClick event of the btnCancel control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="DevExpress.XtraBars.ItemClickEventArgs"/> instance containing the event data.</param>
        private void btnCancel_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) {
            AppConfiguration.Current.LoadData();
            Close();
        }

        /// <summary>
        /// Handles the ItemClick event of the btnReset control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="DevExpress.XtraBars.ItemClickEventArgs"/> instance containing the event data.</param>
        private void btnReset_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) {
            if (MessageBox.Show("Are you shure?", "Reset Preferences", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
                == DialogResult.Yes) {
                AppConfiguration.Current.ResetToDefault();
                Close();
            }
        }
    }
}